import { ts } from "qvog-dsl";

import { ForInStatement, is, native, ObjectLiteralExpression, HomeCheckHint } from 'qvog-dsl';

const SEVERITY = 2;
const DOC_PATH = 'docs/no-for-in-array.md';
const DESCRIPTION = 'For-in loops over arrays skips holes, returns indices as strings, and may visit the prototype chain or other enumerable properties. Use a more robust iteration method such as for-of or array.forEach instead.';

export default native<HomeCheckHint>()()
    .match(ForInStatement)
    .when((node: ts.ForInStatement): boolean => {
        return !is(node.expression, ObjectLiteralExpression);
    })
    .report({
        severity: SEVERITY,
        description: DESCRIPTION,
        docPath: DOC_PATH,
    });

/*
 * The rule above is equivalent with this:

rule()
    .match((node): node is ts.ForInStatement => {
        return ts.isForInStatement(node);
    })
    .when((node: ts.ForInStatement): boolean => {
        return !is(node.expression, ObjectLiteralExpression);
    })
    .report((node: ts.ForInStatement, { api }) => {
        api.report({
            location: api.getLocation(node),
            severity: SEVERITY,
            description: DESCRIPTION,
            docPath: DOC_PATH,
        });
    });
*/